package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.commons.R;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @date 2023/1/10
 */
@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        //1.调用service的新增套餐方法
        setmealService.addSetmealWithDish(setmealDto);
        //2.返回结果
        return R.success("新增成功！");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    String name) {
        //1.调用service的分页查询
        Page<SetmealDto> setmealDtoPage = setmealService.findPage(page, pageSize, name);

        //2.返回分页查询结果
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteSetmeal(@RequestParam Long[] ids) {
        log.info("ids = ", ids.toString());
        //1.调用service的删除方法
        setmealService.deleteWithSetmealDish(ids);

        //2.返回结果
        return R.success("删除成功！");
    }

    /**
     * 查询套餐列表
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //1.根据分类id以及套餐状态进行查询，需要构造查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        //2.调用service的查询列表方法
        List<Setmeal> setmealList = setmealService.list(queryWrapper);

        //3.返回结果
        return R.success(setmealList);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealWithDishs(@PathVariable String id) {
        SetmealDto setmealDto = setmealService.getSetmealWithDishs(id);
        return R.success(setmealDto);
    }

    /**
     *套餐批量启售和禁售
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> setmealStatus(@PathVariable("status")Integer status, @RequestParam Long[] ids) {
        log.info("ids = ", ids.toString());
       //1.查询ids,设置状态
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids!=null, Setmeal::getId,ids);
        List<Setmeal> list = setmealService.list(queryWrapper);
        for (Setmeal setmeal : list) {
            if(setmeal!=null){
                setmeal.setStatus(status);
                setmealService.updateById(setmeal);
            }
        }
        //2.返回结果
        return R.success("修改状态成功！");
    }
}
